package com.pactera.madp.cp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.common.core.constant.CommonConstants;
import com.pactera.madp.cp.api.dto.beta.WorkNewsQueryDTO;
import com.pactera.madp.cp.api.em.beta.ChoreographyEnum;
import com.pactera.madp.cp.api.entity.beta.WorkChoreographyEntity;

import com.pactera.madp.cp.api.vo.beta.*;
import com.pactera.madp.cp.mapper.beta.WorkChoreographyMapper;
import com.pactera.madp.cp.service.WorkChoreographyService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.pactera.madp.cp.common.constant.BetaConstants.CHOREOGRAPHY_TYPE_LIST;

/**
 * @title 精编资讯业务表-业务接口实现
 * @date 2022-06-11
 * @author Deming.Chang
 */
@Service
public class WorkChoreographyServiceImpl extends ServiceImpl<WorkChoreographyMapper, WorkChoreographyEntity> implements WorkChoreographyService {

    @Override
    public WorkChoreographyVO getLatestMorning() {
        return this.cover(baseMapper.selectLatestMorning());
    }

    @Override
    public List<WorkChoreographyVO> getLatestTwo() {
        List<WorkChoreographyVO> targets = new ArrayList<>();
        List<WorkChoreographyEntity> sources = baseMapper.selectLatestTwo();
        sources.forEach(source -> targets.add(this.cover(source)));
        return targets;
    }

    @Override
    public WorkChoreographyVO getByNewsKey(String newsKey) {
        return this.cover(baseMapper.selectByNewsKey(newsKey));
    }

    @Override
    public IPage<WorkChoreographyVO> getPage(Page page, WorkNewsQueryDTO dto) {
        IPage<WorkChoreographyEntity> iPage = baseMapper.selectPage(page, Wrappers.<WorkChoreographyEntity>lambdaQuery()
                .eq(dto.getState() != null, WorkChoreographyEntity::getState, dto.getState())
                .eq(dto.getType() != null, WorkChoreographyEntity::getType, dto.getType())
                .in(dto.getType() == null, WorkChoreographyEntity::getType, CHOREOGRAPHY_TYPE_LIST)
                .eq(dto.getChannel() != null, WorkChoreographyEntity::getChannel, dto.getChannel())
                .apply(StrUtil.isNotBlank(dto.getTitle()),"info ->> '$.title' like ('%"+ dto.getTitle() +"%')")
                .eq(WorkChoreographyEntity::getDelFlag, Integer.parseInt(CommonConstants.STATUS_NORMAL))
        );
        List<WorkChoreographyEntity> recordsSource = iPage.getRecords();
        recordsSource.sort(((o1, o2) -> {
            WorkInfoVO info1 = JSONUtil.toBean(o1.getInfo(), WorkInfoVO.class);
            WorkInfoVO info2 = JSONUtil.toBean(o2.getInfo(), WorkInfoVO.class);
            return info2.getPublishTime().compareTo(info1.getPublishTime());
        }));
        List<WorkChoreographyVO> recordsTarget = new ArrayList<>(recordsSource.size());

        recordsSource.forEach(source -> recordsTarget.add(this.cover(source)));
        page.setRecords(recordsTarget);
        return page;
    }
    /**
     * 将精编中间对象转换为精编数据展示对象
     * @param source    待转换的对象
     * @return          转换后的对象
     */
    private WorkChoreographyVO cover(WorkChoreographyEntity source){
        if(ObjectUtil.isNull(source)){
            return new WorkChoreographyVO();
        }
        WorkChoreographyVO target = new WorkChoreographyVO();
        BeanUtil.copyProperties(source, target);
        target.setInfoSubType(ChoreographyEnum.findByType(source.getType()).getName());
        target.setInfo(JSONUtil.toBean(source.getInfo(), WorkInfoVO.class));
        target.setModuleAndNews(JSONUtil.toList(JSONUtil.parseArray(source.getModuleAndNews()), WorkModuleAndNewsVO.class));
        return target;
    }
}
